package MemberService

import (
	"context"
	"gdshop-font-api-go/app/entity"
	"gdshop-font-api-go/app/request/MemberReq"
	"gdshop-font-api-go/library/response"
	"gdshop-font-api-go/library/tools"
	toolsDb "gdshop-font-api-go/library/tools/db"
	"github.com/gogf/gf/frame/g"
)

func MyBalance(ctx context.Context, parames *MemberReq.MyBalance) *response.JsonResponse {
	obj := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "member_finance").Where("member_id", parames.MemberId)

	if parames.Type == 1 {
		obj.Where("coin > 0")
	}
	if parames.Type == 2 {
		obj.Where("coin < 1")
	}
	mfList := []*entity.MemberFinance{}
	err := obj.Fields("coin,create_time,remark").Page(parames.Page, parames.PageSize).
		OrderDesc("id").Structs(&mfList)
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}
	count, err := obj.Fields("1").Count()
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}

	return response.SuccessByRequestMessageData(nil, "成功", g.Map{
		"page_info": tools.GetPageInfo(count, parames.Page, parames.PageSize),
		"list":      mfList,
	})
}
